# -*- coding: utf-8 -*-

from match import *
from storage import *
import json
import os

if __name__ == '__main__':
    data_util = DataUtil("data")
    extractors = [HuiZuiExtractor, MoneyExtractor, GongTongExtractor, ShuZuiExtractor, DuPinBianHuExtractor,
                  QiangJieBianHuExtractor, GuYiShanHaiBianHuExtractor, ZiShouExtractor, LeiFanExtractor,
                  HuiZuiExtractor,
                  QianKeExtractor, DaoQieBianHuExtractor, WeiXianJiaShiBianHuExtractor, LiGongExtractor,
                  TanBaiExtractor, BeiHaiRenLiangJieExtractor, CangYuExtractor, RenZuiExtractor, ZhuDongPeiChangExtractor,
                  TuiZangExtractor, QiaoZhaBianHuExtractor, WeiXianBianHuExtractor, JiaoTongBianHuExtractor,
                  JiZiZhaPianBianHuExtractor, WeiFanJTFGExtractor, ZuoAnGongJuExtractor, ZuoAnDuiXiangExtractor,
                  GuYiShangHaiExtractor, daoqieExtractor, ZhuGuanEXingExtractor, ZuiJiuJiaShiExtractor]

    file_path, text = data_util.next_line()
    out_dir = "data"
    while text:
        text_json = json.loads(text)
        for extractor in extractors:
            if extractor.is_my_job(file_path):
                text_json.update(extractor.extract("\n".join(text_json["content"])))
        file_dir, file_name = DataUtil.get_file_dir_name(file_path)
        out_file_path = os.path.join(out_dir, file_name + "_extracted.json")
        data_util.append_result(out_file_path, json.dumps(text_json, ensure_ascii=False))
        file_path, text = data_util.next_line()
